package com.java.demo.demo;

public class AutoCloseableDemo {

    public static void main(String[] args) throws Exception {
        AutoCloseableDemo closeableDemo = new AutoCloseableDemo();
        closeableDemo.test();
    }

    private void test() throws Exception {
        try (Demo1 demo1 = new Demo1()) {
             demo1.demo();

            throw new RuntimeException("异常");

        } catch (Exception e) {
            throw e;
        }
    }


    public static class Demo1 implements AutoCloseable {

        public void demo() {
            System.out.println("demo-demo-demo");
        }

        @Override
        public void close() throws Exception {
            System.out.println("自动关闭操作");
        }
    }
}